<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Languages extends MY_Controller {

    public function __construct() {

        parent::__construct();


        $this->data['menu_config'] = $this->menu_config_5;

        $user_session = $this->session->userdata('user');
        $this->data['user_session'] = $user_session;
        if (empty($user_session)) {
            redirect(admin_url('authenticate/login'));
        }
        $this->load_lang('admincp/language');
        $this->lang = $this->lang->language;
        $this->data['lang'] = $this->lang;
        $this->permission();
    }

    public function index() {
        $this->data['laguages'] = $this->languages->getLanguages();
        $this->load_view('languages/index', 'admincp');
    }

    public function form($id = 0) {
        $language = $this->languages->getLanguageById($id);
        if ($language) {
            $this->data['language'] = $language;
        }
        $posts = $this->input->post();

        if ($posts) {

            foreach ($posts as $key => $post) {
                $language->$key = $post;
            }
            $this->data['language'] = $language;

            if ($this->validate($language)) {
                if (empty($language->language_id)) {
                    $user_id = $this->languages->insert($posts);
                    redirect(admin_url('languages'));
                    $this->session->set_flashdata('success', $this->lang['add_success']);
                } else {
                    $this->languages->update($language->language_id, $posts);
                    redirect(admin_url('languages'));
                    $this->session->set_flashdata('success', $this->lang['edit_success']);
                }
            }
        }
        $this->html_form($language);
        $this->load_view('languages/form', 'admincp');
    }

    private function validate($user) {
        $posts = $this->input->post();

        $flag = true;

        if (!$this->hasPermission('modify')) {
            $this->data['error'][] = $this->lang['error_permission_modify'];
            $flag = false;
        }

        if (strlen($posts['name']) == 0) {
            $this->data['error'][] = $this->lang['error_name_empty'];
            $flag = false;
        }
        if (strlen($posts['code']) == 0) {
            $this->data['error'][] = $this->lang['error_code_empty'];
            $flag = false;
        }

        return $flag;
    }

    private function html_form($language) {
        $roles = $this->role->getRoles();
        $roleList = array();
        foreach ($roles as $role) {
            $roleList[$role->id] = $role->name;
        }
        $this->data['form'] = array(
            array(
                'type' => 'input',
                'label' => $this->lang['entry_name'],
                'require' => true,
                'value' => array(
                    'name' => 'name',
                    'id' => 'name',
                    'class' => 'st-forminput',
                    'value' => !empty($language->name) ? $language->name : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_code'],
                'require' => true,
                'value' => array(
                    'name' => 'code',
                    'id' => 'code',
                    'class' => 'st-forminput',
                    'value' => !empty($language->code) ? $language->code : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_locale'],
                'value' => array(
                    'name' => 'locale',
                    'id' => 'locale',
                    'class' => 'st-forminput',
                    'value' => !empty($language->locale) ? $language->locale : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_image'],
                'value' => array(
                    'name' => 'image',
                    'id' => 'image',
                    'class' => 'st-forminput',
                    'value' => !empty($language->image) ? $language->image : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_directory'],
                'value' => array(
                    'name' => 'directory',
                    'id' => 'directory',
                    'class' => 'st-forminput',
                    'value' => !empty($language->directory) ? $language->directory : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_filename'],
                'value' => array(
                    'name' => 'filename',
                    'id' => 'filename',
                    'class' => 'st-forminput',
                    'value' => !empty($language->filename) ? $language->filename : ''
                )
            ),
            array(
                'type' => 'input',
                'label' => $this->lang['entry_sort_order'],
                'value' => array(
                    'name' => 'sort_order',
                    'id' => 'sort_order',
                    'class' => 'st-forminput',
                    'value' => !empty($language->sort_order) ? $language->sort_order : ''
                )
            ),
            array(
                'type' => 'dropdown',
                'label' => $this->lang['entry_status'],
                'value' => array(
                    array(
                        'name' => 'status',
                        'options' => array('1' => 'active', '0' => 'de-active'),
                        'selected' => !empty($user->status) ? $user->status : '',
                        'extra' => 'class="uniform"'
                    ),
                )
            )
        );
        
        $this->data['input_submit'] = array(
            'value' => $this->lang['btn_save'],
            'class' => 'button-blue'
        );
    }

}